UART INTERFACING WITH 8051
Several devices collect data from sensors and need to send it to another unit, like a computer, for further processing. Data transfer/communication is generally done in two ways: parallel and serial.
Synopsis

Several devices collect data from sensors and need to send it to another unit, like a computer, for further processing. Data transfer/communication is generally done in two ways: parallel and serial. In the parallel mode, data transfer is fast and uses more number of lines. This mode is good for short range data transfer. Serial communication on the other hand, uses only one or two data lines to transfer data and is generally used for long distance communication. In serial communication the data is sent as one bit at a time. The operation and description of the 8051 microcontroller to UART serial interface is discussed in this application note. In addition, information for obtaining the source code in C language, containing communication routines between the 8051 core microcontroller (AT89S52) and the UART is provided. The implementation of a microcontroller such as 8051 to UART serial interface and the UART to RS-232 serial port interface.

Description

Serial Port

The microcontroller AT89S52 has an inbuilt UART for carrying out serial communication. The serial communication is done in the asynchronous mode. A serial port, like other PC ports, is a physical interface to establish data transfer between computer and an external hardware or device. This transfer, through serial port, takes place bit by bit. IBM introduced the DB-9 RS-232 version of serial I/O standard, which is most widely used in PCs and several devices. In RS232, high and low bits are represented by flowing voltage ranges:


The range -3V to +3V is undefined. The TTL standards came a long time after the RS232 standard was set. Due to this reason RS232 voltage levels are not compatible with TTL logic. Therefore, while connecting an RS232 to microcontroller system, a voltage converter is required. This converter converts the microcontroller output level to the RS232 voltage levels, and vice versa. IC MAX232, also known as line driver, is very commonly used for this purpose. The simplest connection between a PC and microcontroller requires a minimum of three pins, RxD (receiver, pin2), TxD (transmitter, pin3) and ground (pin5) of the serial port of computer.


TxD pin of serial port connects to RxD pin of controller via MAX232. And similarly, RxD pin of serial port connects to the TxD pin of controller through MAX232. MAX232 has two sets of line drivers for transferring and receiving data. The line drivers used for transmission are called T1 and T2, whereas the line drivers for receiver are designated as R1 and R2. The connection of MAX232 with computer and the controller is shown in the circuit diagram.


An important parameter considered while interfacing serial port is the Baud rate which is the speed at which data is transmitted serially. It is defined as number of bits transmitted or received per second. It is generally expressed in bps (bits per second). AT89S52 microcontroller can be set to transfer and receive serial data at different baud rates using software instructions. Timer1 is used to set the baud rate of serial communication for the microcontroller. For this purpose, Timer1 is used in mode2 which is an 8-bit auto reload mode. (Refer Timer programming with 8051) To get baud rates compatible with the PC, TH1 should be loaded with the values as shown:


In this project, baud rate 9600bps is used. For serial communication AT89C51 has registers SBUF and SCON (Serial control register). SBUF is an 8-bit register. For transmitting a data byte serially, it needs to be placed in the SBUF register. Similarly whenever a data byte is received serially, it comes in the SBUF register, i.e., SBUF register should be read to receive the serial byte. SCON register is used to set the mode of serial communication. The project uses Mode1, in which the data length is of 8 bits and there is a start and a stop bit. The SCON register is bit addressable register. The following table shows the configuration of each bit.


TI (transmit interrupt) is an important flag bit in the SCON register. The controller raises the TI flag when the 8-bit character is transferred. This indicates that the next byte can be transferred now. The TI bit is raised at the beginning of the stop bit.

RI (receive interrupt) is also a flag bit of the SCON register. On receiving the serial data, the microcontroller skips the start and stop bits, and puts the byte is SBUF register. The RI flag bit is then raised to indicate that the byte has been received and should be picked up.

Proteus design for UART interfacing with 8051


Orcad design for UART interfacing with 8051


UART Interfacing with 8051

/*  Name     : main.c
 *  Purpose  : Source code for UART Interfacing with AT89C52.
 *  Author   : Gemicates
 *  Date     : 2017-06-21
 *  Website  : www.gemicates.org
 *  Revision : none
 */
#include <regx52.h>
#include <string.h>

#define lcd P2

sbit rs=P1^0;
sbit rw=P1^1;
sbit e=P1^2;

// LCD FUNCTIONS
			void lcddata(char t);     
			void lcdstring( char *l);
			void cmd(unsigned char);
			void com();

// UART FUNCTIONS
			void init();
			void tdata(unsigned char);
			unsigned char rdata(void);

//DELAY FUNCTION
			void delay(unsigned char);

//MAIN FUNCTION//
			void main()
				{
					unsigned int i,j;
					unsigned char c,*d;
					d="gemicates";                                                // Tranamitted Value
					j=strlen(d);
							P1=0x00;
							P2=0x00;
					init();				                              // UART intialization
					com();				                              // LCD intialization
							cmd(0x80);
							lcdstring("TRANSMITTED CHAR");                // LCD string Print  
                                                                                                      // CHARCTER TRANSMISSION LOOP
							cmd(0xc4);				
									for(i=0;i<j;i++)
									{
										tdata(d[i]);
										lcddata(d[i]);
									}
									delay(1000);
							cmd(0x01);
							cmd(0x82);
							lcdstring("RECEIVED CHAR");                   // LCD string Print 
							cmd(0xc7);
									                              // CHARCTER RECEIVE LOOP
									while(1)  
									{
									c=rdata();
									lcddata(c);
									cmd(0xc7);
									}
				}

//SUB FUNCTIONS//

void delay(unsigned char t)	                                                                      // Delay Function
{
	unsigned int i,j;
	for(i=0;i<t;i++)
	for(j=0;j<1275;j++);
}

void init() 		                                                                              // UART INITIALIZATION
{
	TMOD=0x20;
	TH1=0xFD;
	SCON=0x50;
	TR1=1;
}

void tdata(unsigned char t)                                                                           // DATA Transmission
{
	SBUF=t;
	while(TI==0);
	TI=0;
}

unsigned char rdata(void)	                                                                      // DATA Receive
{
	unsigned char d;
	d=SBUF;
	while(RI==0);
	RI=0;
	return d;
}
  
void lcddata(char t)                                                                                  // LCD Data Function
{
	rs=1;
	lcd=t;	
	rw=0;
	e=1;
	delay(1);
	e=0;
}


void cmd(unsigned char c)	                                                                      // LCD Command Function
{
	lcd=c;
	rs=0;
	rw=0;
	e=1;
	delay(1);
	e=0;
}

void com() 	                                                                                      // LCD Decleration Function				
{
	cmd(0x38);
	delay(10);
	cmd(0x0c);
	delay(10);
	cmd(0x01);
	delay(10);
	cmd(0x80);
	delay(10);
}

void lcdstring(char *l) 	                                                                      // LCD String Function 
{
	while(*l !=0)
	{
		lcddata(*l++);
	}
}

Interfacing of UART with 8051

Error message here!

Show Error message here!


Forgot your password?

Error message here!

Send OTP

Error message here!

Show Error message here!


Lost your password? Please enter your email address. You will receive a password you Need.

Send Error message here!


Back to log-in

Close